from pages.page import Page
from selenium.webdriver.common.by import By
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.common.keys import Keys


class InternalPage(Page):

    @property
    def logout_button(self):
        return self.driver.find_element_by_css_selector("nav a[href $= '?logout']")

    @property
    def user_profile_link(self):
        return self.driver.find_element_by_css_selector("nav a[href $= '?go=profile']")

    @property
    def user_management_link(self):
        return self.driver.find_element_by_css_selector("nav a[href $= '?go=users']")

    @property
    def is_this_page(self):
        return self.is_element_visible((By.CSS_SELECTOR, ".searchbox>#q"))

    @property
    def refresh_button(self):
        return self.driver.find_element_by_css_selector(".center>a>h1")

    @property
    def search_field(self):
        return self.driver.find_element_by_css_selector(".searchbox>#q")

    @property
    def add_movie_button(self):
        return self.driver.find_element_by_xpath(".//a/img[@title='Add movie']")

    @property
    def movie_list(self):
        return self.driver.find_element_by_id('results').find_elements_by_tag_name("a")

    @property
    def is_movie_list_empty(self):
        return self.is_element_visible((By.XPATH, ".//div[@id='results']/div[@class='content'][text()='No movies where found.']"))

    def movie_button(self, movie_name):
        return self.driver.find_element_by_xpath(".//div[@title='" + movie_name + "']")

    def clear_search_field(self):
        ActionChains(self.driver)\
            .click(self.search_field)\
            .send_keys(Keys.CONTROL, 'a')\
            .key_up(Keys.CONTROL)\
            .send_keys(Keys.BACKSPACE, Keys.ENTER)\
            .perform()